﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Text;
using System.Xml.Serialization;

/// <summary>
///  工具类，存放一些经常调用的方法
/// </summary>
public static class ToolsClass
{
    static Transform uiParents;
    public static Transform UiParents
    {
        get
        {
            if (uiParents == null)
            {
                uiParents = GameObject.Find("Canvas").transform;
            }
            return uiParents;
        }
    }


    /// <summary>
    ///  生成Panel的方法
    /// </summary>
    /// <param name="type">生成Panel的面板类型</param>
    /// <returns></returns>
    public static GameObject CreateUIContext(PanelType type)
    {
        GameObject go = Resources.Load<GameObject>(type.ToString());
        if (go == null)
        {
            Debug.Log(type.ToString() + "没找到");
            return null;
        }
        else
        {
            GameObject panel = GameObject.Instantiate(go);
            panel.name = type.ToString();
            //bool  是否需要保持世界坐标
            panel.transform.SetParent(UiParents, false);
            return go;
        }
    }

    /// <summary>
    /// 对牌进行排序处理
    ///      委托和lambda表达式
    /// </summary>
    /// <param name="cards">需要排序的牌</param>
    /// <param name="asc">升序 123 判断</param>
    public static void SortCard(List<Card> cards, bool asc)
    {
        cards.Sort(
             (Card a, Card b) =>
             {
                 if (asc)
                 {
                     return a.CardWeight.CompareTo(b.CardWeight);
                 }
                 else
                 {
                     return -(a.CardWeight.CompareTo(b.CardWeight));
                 }
             }
            );
    }

    /// <summary>
    ///  比较牌的大小
    /// </summary>
    /// <param name="cards">出的牌</param>
    /// <param name="cardType">出牌类型</param>
    /// <returns></returns>
    public static int GetWeight(List<Card> cards, CardType cardType)
    {
        int totalWeight = 0;
        //三带一 /  三带二
        if (cardType == CardType.ThreeAndOne || cardType == CardType.ThreeAndTwo)
        {
            for (int i = 0; i < cards.Count; i++)
            {
                if (cards[i].CardWeight == cards[i + 1].CardWeight && cards[i].CardWeight == cards[i + 2].CardWeight)
                {
                    totalWeight += (int)cards[i].CardWeight;
                    totalWeight *= 3;
                    break;
                }
            }
        }
        //如果是顺子 或者对对顺
        else if (cardType == CardType.Straight || cardType == CardType.DoubleStraight)
        {
            totalWeight = (int)cards[0].CardWeight;
        }
        else
        {
            for (int i = 0; i < cards.Count; i++)
            {
                totalWeight += (int)cards[i].CardWeight;
            }
        }
        return totalWeight;
    }

    /// <summary>
    ///  数据存储
    /// </summary>
    /// <param name="data"></param>
    public static void SaveData(GameDataArgs data)
    {
        Stream mStream = new FileStream(Const.DataPath, FileMode.OpenOrCreate, FileAccess.Write);
        StreamWriter sWrite = new StreamWriter(mStream, Encoding.UTF8);
        XmlSerializer xml = new XmlSerializer(data.GetType());
        xml.Serialize(sWrite, data);

        mStream.Close();
        sWrite.Close();
    }

    /// <summary>
    ///  获取数据
    /// </summary>
    /// <returns></returns>
    public static GameDataArgs GetGameData()
    {
        GameDataArgs data = new GameDataArgs();
        Stream stream = new FileStream(Const.DataPath,FileMode.Open,FileAccess.Read);
        StreamReader sReader = new StreamReader(stream, true);
        XmlSerializer xml = new XmlSerializer(data.GetType());

        data = xml.Deserialize(sReader) as GameDataArgs;
        stream.Close();
        sReader.Close();

        return data;
    }

}
